<?php


namespace app\common\taglib;
use app\common\model\Arctype;
use think\template\TagLib;

class Ht extends TagLib
{
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'channel' => ['attr'=>'typeid,name,length,id,key,mod,empty,titlelen,type'],
        'category' => ['attr'=>'typeid,name,length,id,key,mod,empty,titlelen,type'],
        'channelproduct' => ['attr'=>'name,length,id,typeid,titlelen,key'],
        'article' => ['attr'=>'name,length,id,typeid,titlelen,orderby,type,is_tui'],
        'channelartlist' => ['attr'=>'name,length,id,typeid,titlelen'],
        'product' => ['attr'=>'name,length,id,typeid,titlelen,is_tui'],
        'links' =>['attr'=>'name,length,id,key,mod,empty'],
        'images' =>['attr'=>'name,length,id,key,mod,empty,typeid'],
        'config'=>['attr'=>'name', 'close'=>0],
        'banner' => ['attr'=>'name,length,id'],
        'prenext'=> ['attr' => 'get,empty,id,titlelen'],
        'tags' => ['attr'=>'name,length,id'],
        'notempty'=> ['attr' => 'name'],
        'else'    => ['attr' => '', 'close' => 0],
        'kefu'=> ['attr' => 'length,name,id,group'],
        'hasperm' => ['attr' => 'id'],
        'title' => ['attr'=>'', 'close'=>0]
    ];
    public function tagTitle()
    {
        $parse = '<?php ';
        $parse .= '$tagChannel = new app\common\taglib\ht\tagTitle;';
        $parse .= '$data = $tagChannel->createTitle();';
        $parse .= 'echo $data;';
        $parse .= 'unset($data)';
        $parse .= ' ?>';
        return $parse;
    }
    public function tagHasperm($tag, $content)
    {
        $id = !empty($tag['id']) ? $tag['id'] : '';
        $parse = '<?php ';
        $parse .= 'if (hasPerm(' .$id.')): ?>';
        $parse .= $content . '<?php endif; ?>';
        return $parse;
    }

    // 获取客服
    public function tagKefu($tag, $content)
    {

        $name   = !empty($tag['name']) ? $tag['name'] : '';
        $group   = !empty($tag['group']) ? $tag['group'] : 2;
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 10;

        $parse = '<?php ';

        if ($name) {
            $symbol     = substr($name, 0, 1);
            if (':' == $symbol) {
                $name = $this->autoBuildVar($name);
                $parse .= '$_result=' . $name . ';';
                $name = '$_result';
            } else {
                $name = $this->autoBuildVar($name);
            }
            $parse .= ' $__LIST__ = ' . $name . ';';
        } else {
            $parse .= '$tagChannel = new app\common\taglib\ht\tagKefu;';
            $parse .= '$data = $tagChannel->getKefu('.$group.');';
            $parse .= '$__LIST__ = $data;';
        }
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" id="' . $id . '" length="' . $length . '}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    // 获取图集
    public function tagImages($tag, $content)
    {
        $typeid = !empty($tag['typeid']) ? $tag['typeid'] : 0;
        $name   = !empty($tag['name']) ? $tag['name'] : '';
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $offset = !empty($tag['offset']) && is_numeric($tag['offset']) ? intval($tag['offset']) : 0;
        $key    = !empty($tag['key']) ? $tag['key'] : 'i';
        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 10;
        $mod    = !empty($tag['mod']) && is_numeric($tag['mod']) ? $tag['mod'] : '2';
        $empty  = isset($tag['empty']) ? $tag['empty'] : '';
        $empty  = htmlspecialchars($empty);
        $parse = '<?php ';
        if ($name) {
            $symbol     = substr($name, 0, 1);
            if (':' == $symbol) {
                $name = $this->autoBuildVar($name);
                $parse .= '$_result=' . $name . ';';
                $name = '$_result';
            } else {
                $name = $this->autoBuildVar($name);
            }
            $parse .= ' $__LIST__ = ' . $name . ';';
        } else {
            $parse .= '$tagImages = new app\common\taglib\ht\tagImages;';
            $parse .= '$data = $tagImages->getImages('.$typeid.');';
            $parse .= '$__LIST__ = $data;';
        }
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" id="' . $id . '" mod="' . $mod . '" length="' . $length;
        $parse .= '" offset="' . $offset . '" key="' . $key . '" empty="' . $empty . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    // 获取栏目列表标签
    public function tagChannel($tag, $content)
    {
        $typeid = !empty($tag['typeid']) ? $tag['typeid'] : 0;
        $typeid = $this->varOrvalue($typeid);
        $name   = !empty($tag['name']) ? $tag['name'] : '';
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $offset = !empty($tag['offset']) && is_numeric($tag['offset']) ? intval($tag['offset']) : 0;
        $key    = !empty($tag['key']) ? $tag['key'] : 'i';
        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 10;
        $mod    = !empty($tag['mod']) && is_numeric($tag['mod']) ? $tag['mod'] : '2';
        $titlelen = !empty($tag['titlelen']) && is_numeric($tag['titlelen']) ? intval($tag['titlelen']) : 100;
        $empty  = isset($tag['empty']) ? $tag['empty'] : '';
        $empty  = htmlspecialchars($empty);
        $type   = !empty($tag['type']) ? $tag['type'] : 'top';
        $is_menu   = !empty($tag['is_menu']) ? 1 : 0;
        $parse = '<?php ';

        if ($name) {
            $symbol     = substr($name, 0, 1);
            if (':' == $symbol) {
                $name = $this->autoBuildVar($name);
                $parse .= '$_result=' . $name . ';';
                $name = '$_result';
            } else {
                $name = $this->autoBuildVar($name);
            }
            $parse .= ' $__LIST__ = ' . $name . ';';
        } else {
            $parse .= '$tagChannel = new app\common\taglib\ht\tagChannel;';
            $parse .= '$typeid=' . $typeid.';';
            $parse .= '$data = $tagChannel->getChannel($typeid,"'.$type.'","'.$is_menu.'");';
            $parse .= '$__LIST__ = $data;';
        }
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" id="' . $id . '" mod="' . $mod . '" length="' . $length;
        $parse .= '" offset="' . $offset . '" key="' . $key . '" empty="' . $empty . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    // 是否为空
    public function tagNotempty($tag, $content)
    {
        $name     = $tag['name'];
        $name     = $this->autoBuildVar($name);
        $parseStr = '<?php if(!(empty(' . $name . ') || ((' . $name . ' instanceof \think\Collection || ' . $name . ' instanceof \think\Paginator ) && ' . $name . '->isEmpty()))): ?>' . $content . '<?php endif; ?>';
        return $parseStr;
    }
    // banner调用
    public function tagBanner($tag, $content)
    {
        $name   = !empty($tag['name']) ? $tag['name'] : '';
        $flag   = !empty($tag['flag']) ? $tag['flag'] : '';
        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 15;
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $parse = '<?php ';
        $parse .= '$tagBanner = new app\common\taglib\ht\tagBanner;';
        $parse .= '$data = $tagBanner->getBanner('.$length.',"'.$flag.'");';
        $parse .= '$__LIST__ = $data;';
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" length="' . $length .'" id="' . $id . '"';
        $parse .= '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    // 标签Tags调用
    public function tagTags($tag, $content)
    {
        $name   = !empty($tag['name']) ? $tag['name'] : '';
        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 5;
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $parse = '<?php ';
        $parse .= '$tagTags = new app\common\taglib\ht\tagTags;';
        $parse .= '$data = $tagTags->getTags('.$length.');';
        $parse .= '$__LIST__ = $data;';
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" length="' . $length .'" id="' . $id . '"';
        $parse .= '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    // 获取产品分类
    public function tagCategory($tag, $content)
    {
        $typeid = !empty($tag['typeid']) ? $tag['typeid'] : 0;
        $name   = !empty($tag['name']) ? $tag['name'] : '';
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $offset = !empty($tag['offset']) && is_numeric($tag['offset']) ? intval($tag['offset']) : 0;
        $key    = !empty($tag['key']) ? $tag['key'] : 'i';
        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 10;
        $mod    = !empty($tag['mod']) && is_numeric($tag['mod']) ? $tag['mod'] : '2';
        $titlelen = !empty($tag['titlelen']) && is_numeric($tag['titlelen']) ? intval($tag['titlelen']) : 100;
        $empty  = isset($tag['empty']) ? $tag['empty'] : '';
        $empty  = htmlspecialchars($empty);
        $type   = !empty($tag['type']) ? $tag['type'] : 'son';
        $parse = '<?php ';

        if ($name) {
            $symbol     = substr($name, 0, 1);
            if (':' == $symbol) {
                $name = $this->autoBuildVar($name);
                $parse .= '$_result=' . $name . ';';
                $name = '$_result';
            } else {
                $name = $this->autoBuildVar($name);
            }
            $parse .= ' $__LIST__ = ' . $name . ';';
        } else {
            $parse .= '$tagCategory = new app\common\taglib\ht\tagCategory;';
            $parse .= '$data = $tagCategory->getCategory('.$typeid.',"'.$type.'");';
            $parse .= '$__LIST__ = $data;';
        }
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" id="' . $id . '" mod="' . $mod . '" length="' . $length;
        $parse .= '" offset="' . $offset . '" key="' . $key . '" empty="' . $empty . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    // 文章调用
    public function tagArticle($tag, $content)
    {
        $name   = !empty($tag['name']) ? $tag['name'] : '';
        $orderby   = !empty($tag['orderby']) ? $tag['orderby'] : '';
        $typeid = !empty($tag['category_id']) ? $tag['category_id']: 1;
        $is_tui = !empty($tag['is_tui']) ? $tag['is_tui']: 'all';
        $typeid = $this->varOrvalue($typeid);
        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 5;
        $titlelen = !empty($tag['titlelen']) && is_numeric($tag['titlelen']) ? intval($tag['titlelen']) : 100;
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $type = !empty($tag['type']) ? $tag['type'] : 'self';
        $parse = '<?php ';
        $parse .= '$tagArtlist = new app\common\taglib\ht\tagArticle;';
        $parse .= '$typeid = '.$typeid.';';
        $parse .= '$data = $tagArtlist->getArticle($typeid,'.$length.','.$titlelen.',"'.$orderby.'","'.$type.'","'.$is_tui.'");';
        $parse .= '$__LIST__ = $data;';
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" length="' . $length .'" id="' . $id . '"';
        $parse .= '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    // 父栏目下的子栏目文章调用
    public function tagChannelartlist($tag, $content)
    {
        $name   = !empty($tag['name']) ? $tag['name'] : '';
        $typeid = !empty($tag['typeid']) ? $tag['typeid']: 0;
        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 5;
        $titlelen = !empty($tag['titlelen']) && is_numeric($tag['titlelen']) ? intval($tag['titlelen']) : 100;
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $parse = '<?php ';
        $parse .= '$tagArticle = new app\common\taglib\ht\tagChannelartlist;';
        $parse .= '$data = $tagArticle->getArticle("'.$typeid.'",'.$length.','.$titlelen.');';
        $parse .= '$__LIST__ = $data;';
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" length="' . $length .'" id="' . $id . '"';
        $parse .= '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    // 友情链接
    public function tagLinks($tag, $content)
    {
        $name   = !empty($tag['name']) ? $tag['name'] : '';
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $offset = !empty($tag['offset']) && is_numeric($tag['offset']) ? intval($tag['offset']) : 0;
        $key    = !empty($tag['key']) ? $tag['key'] : 'i';
        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 10;
        $mod    = !empty($tag['mod']) && is_numeric($tag['mod']) ? $tag['mod'] : '2';
        $empty  = isset($tag['empty']) ? $tag['empty'] : '';
        $empty  = htmlspecialchars($empty);
        $parse = '<?php ';

        if ($name) {
            $symbol     = substr($name, 0, 1);
            if (':' == $symbol) {
                $name = $this->autoBuildVar($name);
                $parse .= '$_result=' . $name . ';';
                $name = '$_result';
            } else {
                $name = $this->autoBuildVar($name);
            }
            $parse .= ' $__LIST__ = ' . $name . ';';
        } else {
            $parse .= '$tagLinks = new app\common\taglib\ht\tagLinks;';
            $parse .= '$data = $tagLinks->getLinks();';
            $parse .= '$__LIST__ = $data;';
        }
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" id="' . $id . '" mod="' . $mod . '" length="' . $length;
        $parse .= '" offset="' . $offset . '" key="' . $key . '" empty="' . $empty . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    // 系统变量
    public function tagConfig($tag)
    {
        $name   = !empty($tag['name']) ? $tag['name'] : '';
        $parse = '<?php ';
        $parse .= '$tagConfig = new app\common\taglib\ht\tagConfig;';
        $parse .= '$data = $tagConfig->geConfig("'.$name.'");';
        $parse .= 'echo $data;';
        $parse .= ' ?>';
        return $parse;
    }
    // product 调用
    public function tagProduct($tag, $content)
    {
        $typeid = !empty($tag['typeid']) ? $tag['typeid']: 0;
        $typeid = $this->varOrvalue($typeid);

        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 5;
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $parse = '<?php ';
        $parse .= '$tagProduct = new app\common\taglib\ht\tagProduct;';
        $parse .= '$typeid = '.$typeid.';';
        $parse .= '$data = $tagProduct->getProduct($typeid,'.$tag.');';
        $parse .= '$__LIST__ = $data;';
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" length="' . $length .'" id="' . $id . '"';
        $parse .= '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    public function tagChannelproduct($tag, $content)
    {
        $typeid = !empty($tag['typeid']) ? $tag['typeid']: 0;
        $typeid = $this->varOrvalue($typeid);
        $key   = !empty($tag['key']) ? $tag['key'] : 'i';
        $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 5;
        $titlelen = !empty($tag['titlelen']) && is_numeric($tag['titlelen']) ? intval($tag['titlelen']) : 100;
        $id   = !empty($tag['id']) ? $tag['id'] : 'field';
        $parse = '<?php ';
        $parse .= '$tagProduct = new app\common\taglib\ht\tagChannelproduct;';
        $parse .= '$typeid = '.$typeid.';';
        $parse .= '$data = $tagProduct->getChannelproduct($typeid,'.$length.','.$titlelen.');';
        $parse .= '$__LIST__ = $data;';
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" length="' . $length .'" id="' . $id . '" key="'. $key . '"';
        $parse .= '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    public function tagElse($tag)
    {
        $parseStr = '<?php else: ?>';
        return $parseStr;
    }
    /**
     * 文章内容页 上一篇 下一篇
     *
     */
    public function tagPrenext($tag, $content)
    {
        $get  =  !empty($tag['get']) ? $tag['get'] : 'pre';
        $titlelen = !empty($tag['titlelen']) && is_numeric($tag['titlelen']) ? intval($tag['titlelen']) : 100;
        $id     = isset($tag['id']) ? $tag['id'] : 'field';

        if (isset($tag['empty'])) {
            $style = 1; // 第一种默认标签写法，带属性empty
        } else {
            $style = 2; // 第二种支持判断写法，可以 else
        }

        if (1 == $style) {
            $empty     = isset($tag['empty']) ? $tag['empty'] : '暂无';
            $empty  = htmlspecialchars($empty);

            $parseStr = '<?php ';
            $parseStr .= ' $tagPrenext = new app\common\taglib\ht\tagPrenext;';
            $parseStr .= ' $_result = $tagPrenext->getPrenext("'.$get.'");';
            $parseStr .= 'if(is_array($_result) || $_result instanceof \think\Collection || $_result instanceof \think\Paginator): ';
            $parseStr .= ' $__LIST__ = $_result;';
            $parseStr .= 'if(empty($__LIST__)) : echo htmlspecialchars_decode("' . $empty . '");';
            $parseStr .= 'else: ';
            $parseStr .= '$'.$id.' = $__LIST__;';
            $parseStr .= '$' . $id . '["title"] = mb_substr($' . $id . '["title"], 0, '.$titlelen.');';

            $parseStr .= '?>';
            $parseStr .= $content;
            $parseStr .= '<?php endif; else: echo htmlspecialchars_decode("' . $empty . '");endif; ?>';

        } else {
            $parseStr = '<?php ';
            $parseStr .= ' $tagPrenext = new app\common\taglib\ht\tagPrenext;';
            $parseStr .= ' $_result = $tagPrenext->getPrenext("'.$get.'");';
            $parseStr .= '?>';
            $parseStr .= '<?php if(!empty($_result["title"])): ?>';
            $parseStr .= '<?php $'.$id.' = $_result; ?>';
            $parseStr .= '<?php $' . $id . '["title"] = mb_substr($' . $id . '["title"], 0, '.$titlelen.'); ?>';
            $parseStr .= $content;
            $parseStr .= '<?php endif; ?>';
        }

        $parseStr .= '<?php $'.$id.' = []; ?>'; // 清除变量值，只限于在标签内部使用

        if (!empty($parseStr)) {
            return $parseStr;
        }
        return;
    }

    /**
     * 自动识别构建变量，传值可以使变量也可以是值
     * @access private
     * @param string $value 值或变量
     * @return string
     */
    private function varOrvalue($value)
    {
        $flag  = substr($value, 0, 1);
        if ('$' == $flag || ':' == $flag) {
            $value = $this->autoBuildVar($value);
        } else {
            $value = str_replace('"', '\"', $value);
            $value = '"' . $value . '"';

        }

        return $value;
    }
}